package cn.pconline.autoclub.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.math.NumberUtils;

/**
 *
 * @author chensy
 */
public class DateUtils {

    public static String format(Date date) {
        return format(date, "yyyy-MM-dd");
    }

    public static String formatDetail() {
        return format(getNow(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDetail(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatYYMMDDHM(Date date) {
        return format(date, "yyyy-MM-dd HH:mm");
    }

    public static String formatMMDD(Date date) {
        return format(date, "MM-dd");
    }

    public static int formatData2YMD() {
        return NumberUtils.toInt(format(getNow(), "yyyyMMdd"));
    }

    public static int formatData2YMD(Date date) {
        return NumberUtils.toInt(format(date, "yyyyMMdd"));
    }

    public static String format(Date date, String fmt) {
        if (date == null) {
            return "";
        }
        DateFormat formatter = new SimpleDateFormat(fmt);
        return formatter.format(date);
    }

    public static Date parseDate(String dateStr, String fmt) {
        DateFormat formatter = new SimpleDateFormat(fmt);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException ex) {
            return null;
        }
    }

    /**
     * ***********************取默认value值的结束*****************
     */
    /**
     * ***时间相关函数开始*****
     */
    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getNow() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 获取昨天时间
     *
     * @return
     */
    public static Date getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }

    /**
     * 得到某一天的23：59 时间
     *
     * @param date
     * @return
     */
    public static Date getTodayLast(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 上一个自然周 开始时间
     *
     * @return
     */
    public static Date getLastWeekStart() {
        Calendar cur = Calendar.getInstance();
        cur.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cur.add(Calendar.DAY_OF_YEAR, -7);
        cur.set(Calendar.HOUR_OF_DAY, 0);
        cur.set(Calendar.MINUTE, 0);
        cur.set(Calendar.SECOND, 0);
        return new Date(cur.getTimeInMillis());
    }

    /**
     * 上一个自然周 结束时间
     *
     * @return
     */
    public static Date getLastWeekEnd() {
        Calendar cur = Calendar.getInstance();
        cur.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cur.add(Calendar.DAY_OF_YEAR, -1);
        cur.set(Calendar.HOUR_OF_DAY, 23);
        cur.set(Calendar.MINUTE, 59);
        cur.set(Calendar.SECOND, 59);
        return new Date(cur.getTimeInMillis());
    }

    private DateUtils() {
    }

    /**
     *
     * @return example. 20140410
     */
    public static int getTodayYmd() {
        SimpleDateFormat ymdSdf = new SimpleDateFormat("yyyyMMdd");
        int todayYmd = Integer.parseInt(ymdSdf.format(new Date()));
        return todayYmd;
    }

    /**
     * 取从startYm开始到当前月份结束的月份列表
     *
     * @param startYm
     * @return
     */
    public static List<Integer> getMonthList(int startYm) {
        return getMonthList(startYm, getTodayYmd() / 100);
    }

    public static List<Integer> getMonthList(int startYm, int endYm) {
        List<Integer> ymList = new ArrayList<Integer>();
        for (int ym = startYm; ym <= endYm; ym = getNextYm(ym)) {
            ymList.add(ym);
        }
        return ymList;
    }

    /**
     * 取指定ym的下一月，如201201 -> 201202, 201212 -> 201301
     *
     * @param ym
     * @return
     */
    public static int getNextYm(int ym) {
        int y = ym / 100;
        int m = ym % 100;
        if (m + 1 < 13) {
            return y * 100 + (m + 1);
        } else {
            return (y + 1) * 100 + 1;
        }
    }

    public static String getFormatedTime(Date date) {
        String result = "";
        if (null != date) {
            Date now = new Date();
            long betweenTime = (now.getTime() - date.getTime()) / 1000;  //两个时间之间的差距,单位为秒
            long min60 = 60 * 60;  //60分钟之内
            long hour24 = 24 * 60 * 60; //24小时
            long day3 = 3 * 24 * 60 * 60; // 3天之内
            long sec60 = 60;//60秒

            if (betweenTime <= 0) {
                result = "刚刚";//如果小于0秒，则显示刚刚
            } else if (betweenTime < sec60) {
                result = betweenTime + "秒前";//如果小于60秒，则显示分钟
            } else if (betweenTime >= sec60 && betweenTime <= min60) { //如果小于60分钟，则显示分钟
                result = (Math.round(betweenTime / 60) <= 0 ? 0 : Math.round(betweenTime / 60)) + "分钟前";
            } else if (betweenTime > min60 && betweenTime <= hour24) { //如果大于60分钟，小于24，则显示小时
                result = Math.round(betweenTime / 60 / 60) + "小时前";
            } else if (betweenTime > hour24 && betweenTime <= day3) { //如果大于24，小于3天，则显示天
                result = Math.round(betweenTime / 60 / 60 / 24) + "天前";
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                result = sdf.format(date);
            }
        }
        return result;
    }

    public static String fromateDate(Date date) {
        String time = "";
        int year = date.getYear() + 1900;
        int month = date.getMonth() + 1;
        int dd = date.getDate();
        return year + "年" + month + "月" + dd + "日";
    }

    public static Date createDate(int year) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        return c.getTime();
    }

    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    /**
     * 获得两时间之间的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int compareDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return -1;
        }
        long intervalMilli = date2.getTime() - date1.getTime();
        if (intervalMilli < 0) {
            return -1;
        }
        if (intervalMilli % (24 * 60 * 60 * 1000) == 0) {
            return (int) (intervalMilli / (24 * 60 * 60 * 1000)) + 1;
        }
        return (int) ((intervalMilli + (24 * 60 * 60 * 1000 - 1)) / (24 * 60 * 60 * 1000));
    }

    /**
     * 获得几天后的日期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date getDateAfter(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + day);
        return c.getTime();
    }

    /**
     * 获得各时间后的具体时间
     *
     * @param date 日期
     * @param day 天数
     * @param hour	小时数
     * @param minute分钟数
     * @param second秒数
     * @return
     */
    public static Date getDateAfter(Date date, int day, int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + day);
        c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + hour);
        c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minute);
        c.set(Calendar.SECOND, c.get(Calendar.SECOND) + second);
        return c.getTime();
    }

    /**
     * 获取几天前的所有日期（包括当天） 格式：20141231,
     *
     * @param date
     * @param fDay
     * @return
     */
    public static List<Integer> getDateBefore(Date date, int fDay) {
        List<Integer> list = new ArrayList<Integer>();
        if (fDay < 1) {
            return list;
        }
        Calendar c = Calendar.getInstance();
        for (int i = fDay - 1; i >= 0; i--) {
            c.setTime(date);
            c.set(Calendar.DATE, c.get(Calendar.DATE) - i);
            int day = formatData2YMD(c.getTime());
            list.add(day);

        }
        return list;
    }

    /**
     * 获取几天前的所有日期（包括当天）
     *
     * @param date
     * @param num
     * @return
     */
    public static List<Date> getDateListBefore(Date fromDate, int num) {
        List<Date> list = new ArrayList<Date>();
        if (num < 1) {
            return list;
        }
        Calendar c = Calendar.getInstance();
        for (int i = num - 1; i >= 0; i--) {
            c.setTime(fromDate);
            c.set(Calendar.DATE, c.get(Calendar.DATE) - i);
            list.add(c.getTime());
        }
        return list;
    }

    /**
     * 获得几天前的日期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date getSingleDateBefore(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DATE, c.get(Calendar.DATE) - day);
        return c.getTime();
    }

    public static void main(String[] args) {
        String time = fromateDate(new Date());
        System.out.println(time);
    }
}